import {
  OnGatewayConnection,
  OnGatewayDisconnect,
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
} from '@nestjs/websockets';
import axios from 'axios';
import { Server, Socket } from 'socket.io';

@WebSocketGateway({
  cors: {
    origin: '*',
  },
})
export class WebsocketGateway
  implements OnGatewayConnection, OnGatewayDisconnect
{
  @WebSocketServer() server: Server;

  handleConnection(client: Socket) {
    // console.log(`Client connected: ${client.id}`);
  }

  handleDisconnect(client: Socket) {
    // console.log(`Client disconnected: ${client.id}`);
  }

  @SubscribeMessage('sendDocument')
  handleSendDocument(
    client: Socket,
    payload: { docId: string; content: string },
  ) {
    this.server.emit('receiveDocument', payload);
  }

  // @SubscribeMessage('sendLLMMessage')
  // async handleSendLLMMessage(
  //   client: Socket,
  //   payload: { docId: string; content: string },
  // ) {
  //   console.log('测试');
  //   const data = {
  //     model: 'phi3:latest',
  //     prompt: 'Why is the sky blue?'
  //   };
  //   console.log(`${process.env.VITE_LLM_URL}/api/generate`);
    
  //   const res = await  axios
  //     .post(`${process.env.VITE_LLM_URL}/api/generate`, {data})
  //   // this.server.emit('receiveLLMMessage', payload);
  // }
}
